﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmLogin : Form
    {
        frmSplash Splasher;
        public frmLogin()
        {
            InitializeComponent();
            txtServer.Text = Properties.Settings.Default.serverName;
            txtServer.TabStop = false;

            txtName.Text = Properties.Settings.Default.LastUser;
            txtPassWord.Focus();

        }
        private bool CheckValid()
        {
            bool checkOK = true;
            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Tên đăng nhập không được trống!", "Thông báo");
                return false;

            }
            if (txtPassWord.Text.Trim().Length == 0)
            {
                MessageBox.Show("Mật khẩu không được trống!", "Thông báo");
                return false;

            }
            return checkOK;
        }
        private bool CheckUser()
        {
            bool checkOK = true;
            try
            {
                //if (Properties.Settings.Default.SalesVMTConn.Length == 0)
                //{
                    Properties.Settings.Default.serverName = txtServer.Text;
                    Properties.Settings.Default.SalesVMTConn = "Data Source=" + txtServer.Text + ";Initial catalog = VMTSales; Integrated Security=True";
                    Properties.Settings.Default.BKSalesVMTConn = "Data Source=" + txtServer.Text + ";Initial catalog = VMTBKSales;  Integrated Security=True";
                //}
                checkOK = UsersDB.CheckValidate(txtName.Text, txtPassWord.Text);
            }
            catch (Exception ex)
            {
                if (ex.ToString().Contains("Error Locating Server/Instance Specified"))
                    MessageBox.Show("Tên máy chủ không chính xác. Vui lòng nhập lại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            Program.strUserName = txtName.Text.Trim();
            Program.strPassword = txtPassWord.Text.Trim();
            if (UsersDB.GetInfoByUsersName(Program.strUserName).Users_Roles == "Cao nhất")
                Program.isAdmin = true;
            Properties.Settings.Default.LastUser = txtName.Text;
            Properties.Settings.Default.Save();

            return checkOK;
        }
        void bgJ_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = CheckUser();
        }
        void bgJ_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Convert.ToBoolean(e.Result))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            Splasher.Close();
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {

            if (!CheckValid())

                return;

            BackgroundWorker bgJ = new BackgroundWorker();

            bgJ.DoWork += new DoWorkEventHandler(bgJ_DoWork);

            bgJ.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgJ_RunWorkerCompleted);

            bgJ.RunWorkerAsync();

            Splasher = new frmSplash("Đang kiểm tra thông tin đăng nhập...");

            Splasher.Show();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}